package com.lonely.fx;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

/**
 * @date: 2022-07-07 11:25
 * @author: ztkj-hzb
 * @dscription: 设置单选和多选，主要用到了类如下：
 *  单选：ToggleGroup RadioMenuItem
 *  多选：CheckMenuItem
 **/
public class 单选多选菜单项设置 extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    /**
     *
     */
    @Override
    public void start(Stage primaryStage) throws Exception {


        BorderPane borderPane = new BorderPane();

        MenuBar menuBar = new MenuBar();
        menuBar.setStyle("-fx-background-color: pink");


        Menu menu1 = new Menu("单选菜单");
        Menu menu2 = new Menu("多选菜单");


        //TODO 单选需放在一个组中
        ToggleGroup toggleGroup = new ToggleGroup();
        RadioMenuItem menuItem1 = new RadioMenuItem("篮球");

        //设置默认选中
        menuItem1.setSelected(true);

        RadioMenuItem menuItem2 = new RadioMenuItem("足球");
        RadioMenuItem menuItem3 = new RadioMenuItem("羽毛球");
        menuItem1.setToggleGroup(toggleGroup);
        menuItem2.setToggleGroup(toggleGroup);
        menuItem3.setToggleGroup(toggleGroup);

        menu1.getItems().addAll(menuItem1, menuItem2, menuItem3);


        //TODO 设置多选
        CheckMenuItem checkMenuItem1 = new CheckMenuItem("篮球");

        //设置默认选中
        checkMenuItem1.setSelected(true);

        CheckMenuItem checkMenuItem2 = new CheckMenuItem("足球");
        CheckMenuItem checkMenuItem3 = new CheckMenuItem("羽毛球");
        menu2.getItems().addAll(checkMenuItem1, checkMenuItem2, checkMenuItem3);


        //设置事件
        //单选按钮事件
        EventHandler<ActionEvent> radioEvent = event -> {
            //输出三个单选的状态
            System.out.printf("单选按钮：篮球 %s 选中%n", menuItem1.isSelected() ? "已经" : "未");
            System.out.printf("单选按钮：足球 %s 选中%n", menuItem2.isSelected() ? "已经" : "未");
            System.out.printf("单选按钮：羽毛球 %s 选中%n", menuItem3.isSelected() ? "已经" : "未");
        };
        menuItem1.setOnAction(radioEvent);
        menuItem2.setOnAction(radioEvent);
        menuItem3.setOnAction(radioEvent);

        //多选按钮事件
        EventHandler<ActionEvent> checkEvent = event -> {
            //输出三个多选的状态
            System.out.printf("多选按钮：篮球 %s 选中%n", checkMenuItem1.isSelected() ? "已经" : "未");
            System.out.printf("多选按钮：足球 %s 选中%n", checkMenuItem2.isSelected() ? "已经" : "未");
            System.out.printf("多选按钮：羽毛球 %s 选中%n", checkMenuItem3.isSelected() ? "已经" : "未");
        };
        checkMenuItem1.setOnAction(checkEvent);
        checkMenuItem2.setOnAction(checkEvent);
        checkMenuItem3.setOnAction(checkEvent);



        menuBar.getMenus().addAll(menu1, menu2);
        borderPane.setTop(menuBar);
        primaryStage.setScene(new Scene(borderPane));
        primaryStage.setWidth(600);
        primaryStage.setHeight(500);
        primaryStage.show();

    }
}
